﻿using Microsoft.TeamFoundation.Controls.WPF;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using TeamExplorer.Merge.Base;
using System.ComponentModel;
using System.Drawing;

namespace TeamExplorer.Merge.MergeCandidates
{
    /// <summary>
    /// Interaction logic for MergeCandidatesPageView.xaml
    /// </summary>
    public partial class MergeCandidatesPageView : UserControl
    {
        private BitmapImage infoIcon;
        private BitmapImage successfulIcon;
        private BitmapImage errorIcon;
        private BitmapImage discardIcon;

        public MergeCandidatesPageView()
        {
            InitializeComponent();
            
            infoIcon = CommonIcons.Instance.GetImage(CommonIcons.Icons.Information);
            successfulIcon = CommonIcons.Instance.GetImage(CommonIcons.Icons.Successful);
            errorIcon = CommonIcons.Instance.GetImage(CommonIcons.Icons.Error);
            discardIcon = new BitmapImage(new Uri("pack://application:,,,/TeamExplorer.Merge;component/Resources/discard.png"));
        }

        public BitmapImage InformationIcon
        {
            get
            {
                return infoIcon;
            }
        }

        public BitmapImage SuccessfulIcon
        {
            get
            {
                return successfulIcon;
            }
        }

        public BitmapImage ErrorIcon
        {
            get
            {
                return errorIcon;
            }
        }

        public BitmapImage DiscardIcon
        {
            get
            {
                return discardIcon;
            }
        }

        public void SetViewModel(object viewModel)
        {
            this.DataContext = viewModel;
            SetupContextMenu();
            SortCommand = new DelegateCommand(ExecuteSort);
        }
                
        private void SetupContextMenu()
        {
            ShowSourceBranchDropDownCommand = new DelegateCommand(ExecuteShowSourceBranchDropDown);
            sourceBranchLink.DropDownMenu.PlacementTarget = sourceBranchLink;
        }

        private void ExecuteShowSourceBranchDropDown(object obj)
        {
            sourceBranchLink.DropDownMenu.IsOpen = !sourceBranchLink.DropDownMenu.IsOpen; //toggle
        }

        private void ExecuteSort(object obj)
        {
            var vs = obj as CollectionViewSource;
            if (vs == null || vs.View.IsEmpty || !vs.View.CanSort || vs.View.SortDescriptions == null || vs.View.SortDescriptions.Count != 1) return;
            
            var sortDescription = vs.View.SortDescriptions[0];

            using (vs.DeferRefresh())
            {
                vs.View.SortDescriptions.RemoveAt(0);
                ListSortDirection direction;
                if (sortDescription.Direction == ListSortDirection.Ascending)
                {
                    direction = ListSortDirection.Descending;
                    sortLink.Text = "desc";
                }
                else
                {
                    direction = ListSortDirection.Ascending;
                    sortLink.Text = "asc";
                }
                var newSortDescription = new SortDescription(sortDescription.PropertyName, direction);
                vs.View.SortDescriptions.Add(newSortDescription);
            }
        }
        
        #region Commands

        public ICommand ShowSourceBranchDropDownCommand
        {
            get;
            private set;
        }

        public ICommand ShowTargetBranchDropDownCommand
        {
            get;
            private set;
        }

        public ICommand SortCommand
        {
            get;
            private set;
        }

        #endregion
    }
}
